package com.ktg.mes.md.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.ktg.mes.md.mapper.MdBatchContentMapper;
import com.ktg.mes.md.domain.MdBatchContent;
import com.ktg.mes.md.service.IMdBatchContentService;



/**
 * 产品批次单流水号明细Service业务层处理
 *
 * @author byteco
 * @date 2022-12-15
 */
@Service
public class MdBatchContentServiceImpl extends ServiceImpl<MdBatchContentMapper, MdBatchContent> implements IMdBatchContentService
{




    /**
     * 查询产品批次单流水号明细
     *
     * @param entity 产品批次单流水号明细
     * @param page 分页对象
     * @return 产品批次单流水号明细
     */
    @Override
    public IPage<MdBatchContent> queryPageList(MdBatchContent entity, Page<MdBatchContent> page) {
        QueryWrapper<MdBatchContent> lqw = new QueryWrapper<MdBatchContent>(entity);
        lqw.orderByDesc("create_time");
        IPage<MdBatchContent> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询产品批次单流水号明细
     *
     * @param entity 产品批次单流水号明细
     * @return 产品批次单流水号明细
     */
    @Override
    public List<MdBatchContent> queryList(MdBatchContent entity) {
        QueryWrapper<MdBatchContent> lqw = new QueryWrapper<MdBatchContent>(entity);
        lqw.orderByDesc("create_time");
        return this.list(lqw);
    }

    public MdBatchContent getInfo(Long id)
    {
        return this.getById(id);
    }
    /**
     * 查询最后一条记录
     */
    public MdBatchContent lastOne(String batchNo){
        QueryWrapper<MdBatchContent> lqw = new QueryWrapper<>();
        lqw.eq("batch_no",batchNo);
        lqw.orderByDesc("create_time");
        lqw.last(" limit 0,1");
        return getOne(lqw);
    }


    /**
     * 新增产品批次单流水号明细
     *
     * @param entity 产品批次单流水号明细
     * @return 结果
     */
    @Override
    public void insert(MdBatchContent entity)
    {
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        this.save(entity);
    }

    /**
     * 修改产品批次单流水号明细
     *
     * @param entity 产品批次单流水号明细
     * @return 结果
     */
    @Override
    public boolean update(MdBatchContent entity)
    {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(entity);
    }

    /**
     * 批量删除产品批次单流水号明细
     *
     * @param recordIds 需要删除的产品批次单流水号明细主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] recordIds)
    {

        return this.removeByIds(Arrays.asList(recordIds));
    }

    /**
     * 删除产品批次单流水号明细信息
     *
     * @param recordId 产品批次单流水号明细主键
     * @return 结果
     */
    @Override
    public boolean delete(Long recordId)
    {
        return this.removeById(recordId);
    }

    private LambdaQueryWrapper<MdBatchContent> buildQueryWrapper(MdBatchContent entity) {
        LambdaQueryWrapper<MdBatchContent> lqw = Wrappers.lambdaQuery();
                    lqw.eq(entity.getBatchHeaderId() != null, MdBatchContent::getBatchHeaderId, entity.getBatchHeaderId());
                    lqw.like(StringUtils.isNotBlank(entity.getItemName()), MdBatchContent::getItemName, entity.getItemName());
                    lqw.eq(StringUtils.isNotBlank(entity.getSpecification()), MdBatchContent::getSpecification, entity.getSpecification());
                    lqw.eq(StringUtils.isNotBlank(entity.getUnitOfMeasure()), MdBatchContent::getUnitOfMeasure, entity.getUnitOfMeasure());
                    lqw.eq(StringUtils.isNotBlank(entity.getViscosity()), MdBatchContent::getViscosity, entity.getViscosity());
                    lqw.eq(StringUtils.isNotBlank(entity.getTag()), MdBatchContent::getTag, entity.getTag());
                    lqw.eq(StringUtils.isNotBlank(entity.getPackingType()), MdBatchContent::getPackingType, entity.getPackingType());
                    lqw.eq(StringUtils.isNotBlank(entity.getBatchNo()), MdBatchContent::getBatchNo, entity.getBatchNo());
                    lqw.eq(StringUtils.isNotBlank(entity.getPrefix()), MdBatchContent::getPrefix, entity.getPrefix());
                    lqw.eq(entity.getSerialNumber() != null, MdBatchContent::getSerialNumber, entity.getSerialNumber());
                    lqw.eq(entity.getPrintNumber() != null, MdBatchContent::getPrintNumber, entity.getPrintNumber());
        return lqw;
    }


    public MdBatchContent findLastSerialNumber(MdBatchContent mdBatchContent){
        
        return getBaseMapper().findLastSerialNumber(mdBatchContent);
    };
}
